/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulador;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JLabel;

/**
 *
 * @author rangel
 */
public class Goal {

	private int width, height;
	private Color color = Color.black;
	private ArrayList<Point> breadcrumbs;
	static int tailSize = 10;
	public char[] motoresAtivos;
	private String type;
	private float xPower;
	private float yPower;
	public float xPosition;
	public float yPosition;
	private float repulsionVector[] = new float[2];
	private Scenario scenario;
	private JLabel image = new JLabel();
	static boolean jaInseriu = false;

	public void setScenario(Scenario scenario) {
		this.scenario = scenario;
	}

	public void removeLabel()
	{
		scenario.remove(image);
		jaInseriu = false;
	}

	public Goal(Point position, int width, int height, String type) {
		motoresAtivos = new char[2]; // Motores nos eixos X e Y
		for (int i = 0; i < motoresAtivos.length; i++) {
			motoresAtivos[i] = 0; // motor desligado
		}
		this.type = type;
		if (type.equals("agent")) {
			breadcrumbs = new ArrayList<Point>();
		}
	    xPosition = position.x;
	    yPosition = position.y;
		this.height = height;
		this.width = width;
	}
	
	public Goal(Point position, int width, int height, String type, int cellSpace, Scenario scenario, String icon) {
		motoresAtivos = new char[2]; // Motores nos eixos X e Y
		xPosition = position.x;
		yPosition = position.y;
		for (int i = 0; i < motoresAtivos.length; i++) {
			motoresAtivos[i] = 0; // motor desligado
		}
		this.type = type;
		if (type.equals("agent")) {
			breadcrumbs = new ArrayList<Point>();
		}
		this.height = height;
		this.width = width;
		this.scenario = scenario;
		if(icon != null) {
			image.setIcon(new javax.swing.ImageIcon(getClass().getResource(icon)));
			image.setVerticalAlignment(image.TOP);
			image.setHorizontalAlignment(image.LEFT);
			image.setBounds((int)xPosition-scenario.cellSpace/2, (int)yPosition-scenario.cellSpace/2, scenario.cellSpace, scenario.cellSpace);
			if(!jaInseriu){
				this.scenario.add(image);
				jaInseriu = true;
			}
		}
	}

	public ArrayList<Point> getBreadCrumbs(){
		return breadcrumbs;
	}

/*	public void drawForGame()
	{
		
	}*/
	public void draw(Graphics2D g2d, boolean showRadar) {
		if (type.equals("agent")) {
			g2d.setColor(Color.red);
			for (int i = 0; i < breadcrumbs.size(); i++) {
				Point p = breadcrumbs.get(i);
				g2d.fillOval(p.x-3, p.y-3, 6, 6);
			}
			g2d.setColor(Color.orange);
		}
		g2d.setColor(color);
		g2d.fillOval((int)(xPosition-width/2), (int)(yPosition-height/2), width, height);
		g2d.setStroke(new BasicStroke(3.0f));
		g2d.setColor(Color.lightGray);
		g2d.drawOval((int)(xPosition-width/2), (int)(yPosition-height/2), width, height);
		g2d.setStroke(new BasicStroke(1.0f));
		g2d.setColor(color);
	}

	public void updatePosition(Point point) {
		xPosition = point.x;
		yPosition = point.y;
		image.setBounds((int)xPosition-scenario.cellSpace/2, (int)yPosition-scenario.cellSpace/2, scenario.cellSpace, scenario.cellSpace);
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public float getXPosition() {
		return xPosition;
	}

	public float getYPosition() {
		return yPosition;
	}

	void updatePosition() {
		if (type.equals("agent")) {
			if (motoresAtivos[0] == 0) // y
			{ // desacelera
				if (yPower > 0) {
					yPower -= 0.5;
				} else if (yPower < 0) {
					yPower += 0.5;
				}
			} else { // tenta acelerar
				if (motoresAtivos[0] == 'u' &&
								yPower > Bot.minPower) {
					yPower -= 0.5;
				} else if (motoresAtivos[0] == 'd' &&
								yPower < Bot.maxPower) {
					yPower += 0.5;
				}
			}
			if (motoresAtivos[1] == 0) // x
			{ // desacelera
				if (xPower > 0) {
					xPower -= 0.5;
				} else if (xPower < 0) {
					xPower += 0.5;
				}
			} else { // tenta acelerar
				if (motoresAtivos[1] == 'r' &&
								xPower < Bot.maxPower) {
					xPower += 0.5;
				} else if (motoresAtivos[1] == 'l' &&
								xPower > Bot.minPower) {
					xPower -= 0.5;
				}
			}
		}
		xPosition += xPower + repulsionVector[0];
		yPosition += yPower + repulsionVector[1];
		image.setBounds((int)xPosition, (int)yPosition, 100, 100);
		repulsionVector[0] = repulsionVector[1] = 0;
		if (type.equals("agent")) {
			breadcrumbs.add(new Point((int) xPosition, (int) yPosition));
			if (breadcrumbs.size() > tailSize) {
				breadcrumbs.remove(0);
			}
		}
	}
}
